﻿using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MRecorder
{
    public partial class MainForm : Form
    {
        enum eColums
        {
            COL_LINE_ID = 0,
            COL_LINE_TYPE,
            COL_STATE,
            COL_CALLER,
            COL_DTMF,
            COL_MAX,
        }
        enum EnumStep
        {
            STEP_IDLE = 0,
            STEP_WELCOME,
            STEP_INPUT_COUNT,
            STEP_INPUT_PASS,
            STEP_CONSULT_DETAIL,
            STEP_GOODBY,
        }

        ArrayList ChannelArray;

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            if (Phonic_usb.OpenDevice(0) != 0)
            {
                MessageBox.Show("Open Device Error!");
                return;
            }

//            Phonic_usb.OpenLogFile(0);
            ChannelArray = new ArrayList();

            Linelist.GridLines = true; //显示表格线
            Linelist.View = View.Details;//显示表格细节
            Linelist.FullRowSelect = true;//是否可以选择行
            Linelist.Columns.Add("ID", 60);
            Linelist.Columns.Add("类型", 60);
            Linelist.Columns.Add("状态", 60);
            Linelist.Columns.Add("主叫", 100);
            Linelist.Columns.Add("DTMF", 100);
        }
        private void MainForm_Closing(object sender, FormClosingEventArgs e)
        {
//            Phonic_usb.CloseLogFile();
            Phonic_usb.CloseDevice();
        }

        private Channel GetChannel(int handle)
        {
            foreach (Channel TheChannel in ChannelArray)
            {
                if (TheChannel._handle == handle)
                {
                    return TheChannel;
                }
            }
            return null;
        }

        private void OnPlugIn(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_PLugInEvent e)
        {
            Channel ThisChannel = new Channel();
            ThisChannel.Inital(e.uboxHandle, ChannelArray.Count);
            int lineType = Phonic_usb.GetProductID(e.uboxHandle);
            string strLineType = "";
            switch (lineType)
            {
                case 1:
                    strLineType = "FI3102A";
                    break;
                case 2:
                    strLineType = "FI3002B";
                    break;
                case 3:
                    strLineType = "FI3001B";
                    break;
                case 4:
                    strLineType = "FI3101A";
                    break;
                default:
                    strLineType = "unkown product";
                    break;
            }

            ListViewItem p = new ListViewItem(new string[] { ChannelArray.Count.ToString(), strLineType, "空闲", "", "" });
            p.Tag = ThisChannel;
            Linelist.Items.Add(p);

            ChannelArray.Add(ThisChannel);

        }

        private void OnPlugOut(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_PlugOutEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                foreach (ListViewItem item in Linelist.Items)
                {
                    if ((Channel)item.Tag == ThisChannel)
                    {
                        item.Remove();
                        ChannelArray.Remove(ThisChannel);
                        break;
                    }
                }
                for (int i = 0; i < Linelist.Items.Count; i++)
                {
                    Linelist.Items[i].SubItems[(int)eColums.COL_LINE_ID].Text = i.ToString();
                }
            }
        }
        private void OnDeviceAlarm(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_DeviceAlarmEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                string strMsg = "UBOX ";
                strMsg += ThisChannel._lineID.ToString();
                switch (e.param)
                {
                    case 1:
                        strMsg += "未能找到ubox 的MIC 设备!";
                        break;
                    case 2:
                        strMsg += "未能打开ubox 的MIC 设备!";
                        break;
                    case 3:
                        strMsg += "未能打开ubox 的放音设备!";
                        break;
                    case 4:
                        strMsg += "设备故障!";
                        break;
                }
                MessageBox.Show(strMsg);

            }
        }
        private void OnDeviceError(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_DeviceErrorEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                string strMsg = "UBOX ";
                strMsg += ThisChannel._lineID.ToString();
                strMsg += " 设备错误!";
                MessageBox.Show(strMsg);

            }
        }


        private void OnRing(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_RingEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                ThisChannel._ringCount++;
                string strTemp = "振铃 ";

                strTemp += ThisChannel._ringCount.ToString();
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = strTemp;
            }
        }

        private void OnRingCancel(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_RingCancelEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "空闲";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
                ThisChannel._ringCount = 0;
            }
        }
        private void OnCallId(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_CallIdEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = e.callerNumber;
                ThisChannel._step = (int)EnumStep.STEP_WELCOME;
            }
        }


        private void OnDtmfKey(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_DtmfKeyEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                ThisChannel._strDTMF += (char)e.dtmfCode;
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = ThisChannel._strDTMF;
            }
        }


        private void OnPlayEnd(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_PlayEndEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "PlayEnd";
            }
        }

        private void OnHangup(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_HangUpEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "空闲";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
                ThisChannel._ringCount = 0;

                if (ThisChannel._bRecording == true)
                {
                    Phonic_usb.StopRecord(ThisChannel._handle);
                    ThisChannel._bRecording = false;
                    //把录音开始时间,录音结束时间,录音时长,按键(呼出号码),呼入号码,录音文件名等等写入数据库.
                }
            }

        }
        private string GetFileName(int lineID)
        {
            System.DateTime currentTime=System.DateTime.Now; 

            string strName;
            string strDirectionary;
            
            strName = currentTime.Year.ToString("d4");
            strDirectionary = strName;

            strName += "-";
            strName += currentTime.Month.ToString("d2");
            strDirectionary += "\\";
            strDirectionary += currentTime.Month.ToString("d2");

            strName += "-";
            strName += currentTime.Day.ToString("d2");
            strDirectionary += "\\";
            strDirectionary += currentTime.Day.ToString("d2");

            strDirectionary += "\\";
            strDirectionary += lineID.ToString("d3");
            Directory.CreateDirectory(strDirectionary);
            
            strDirectionary += "\\";

            strName += "-";
            strName += currentTime.Hour.ToString("d2");
            

            strName += "-";
            strName += currentTime.Minute.ToString("d2");
            

            strName += "-";
            strName += currentTime.Second.ToString("d2");


            strName += "-";
            strName += currentTime.Millisecond.ToString("d2");

            strName += "-";
            strName += lineID.ToString("d3");

            strName += ".wav";

            return strDirectionary + strName;
        }

        private void OnHookOff(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_HookOffEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "摘机";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_CALLER].Text = "";
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_DTMF].Text = "";
                ThisChannel._bRecording = true;
                string strFileName = GetFileName(ThisChannel._lineID);
                Phonic_usb.RecordFile(ThisChannel._handle, strFileName, 0);
            }

        }

        private void OnCallOutFinish(object sender, AxPHONIC_USBLib._DPhonic_usbEvents_CallOutFinishEvent e)
        {
            Channel ThisChannel = GetChannel(e.uboxHandle);
            if (ThisChannel != null)
            {
                Linelist.Items[ThisChannel._lineID].SubItems[(int)eColums.COL_STATE].Text = "呼叫完成";
            }
        }

        private void CallButton_Click(object sender, EventArgs e)
        {
            if ((ChannelArray.Count > 0) && (PhoneNumBox.Text.Length>0))
            {
                Channel ThisChannel = ((Channel)ChannelArray[0]);
                Phonic_usb.SendDtmf(ThisChannel._handle, PhoneNumBox.Text);
            }

        }




    }
}
